﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Monopoly
{
    public partial class throw_start : Form
    {

        private  int dice1;
        private int dice2;
        private int highest_throw = 0;
        private int startplayer = 0;
        private int currentplayer = 0;
        private List<Player> t_players;
        private List<int> thesame = new List<int>();
        private int same = 0;
        public throw_start(List<Player> players)
        {
            InitializeComponent();
            t_players = players;
     
        }

        internal void ThrowDice()
        {
            //randomly generates two throws between 1 and 6 and the sum of these two will be returned.
            Random random = new Random();
            dice1 = random.Next(1, 6);
            dice2 = random.Next(1, 6);
        }

        private void btnThrowDice_Click(object sender, EventArgs e)
        {
            if (currentplayer < t_players.Count)
            {
                lblspeler.Text = t_players[currentplayer].Name;
                ThrowDice();
                pbDice1.ImageLocation = "images/dice" + dice1 + ".png";
                pbDice2.ImageLocation = "images/dice" + dice2 + ".png";
                int total_throw = dice1 + dice2;
                if (total_throw > highest_throw)
                {
                    lblspeler1.Text = t_players[currentplayer].Name;
                    pbDice3.ImageLocation = "images/dice" + dice1 + ".png";
                    pbDice4.ImageLocation = "images/dice" + dice2 + ".png";
                    lblspeler1.Visible = true; pbDice3.Visible = true; pbDice4.Visible = true;
                    highest_throw = total_throw;
                    startplayer = currentplayer;
                }
                else if (total_throw == highest_throw)
                {
                    thesame.Add(currentplayer);
                    thesame.Add(startplayer);
                    same++;   
                }
                currentplayer++;
            }
            else if (same > 0)
            {
                currentplayer = thesame[0];
                highest_throw = 0;
                lblspeler.Text = t_players[currentplayer].Name;
                pbDice1.ImageLocation = "images/dice" + dice1 + ".png";
                pbDice2.ImageLocation = "images/dice" + dice2 + ".png";
                int total_throw = dice1 + dice2;
                if (total_throw > highest_throw)
                {
                    lblspeler1.Text = t_players[currentplayer].Name;
                    pbDice3.ImageLocation = "images/dice" + dice1 + ".png";
                    pbDice4.ImageLocation = "images/dice" + dice2 + ".png";
                    lblspeler1.Visible = true; pbDice3.Visible = true; pbDice4.Visible = true;
                    highest_throw = total_throw;
                    startplayer = currentplayer;
                }
                else if (total_throw == highest_throw)
                {
                    thesame.Add(currentplayer);
                    thesame.Add(startplayer);
                    same++;
                }
                currentplayer++;
                thesame.RemoveAt(0);
                same--;
            }
            else
            {
                t_players[startplayer].Turn = true;
                new Form1(t_players).Show();
            }
        }
    }
}
